package com.example.scity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.scity.beens.LoadResponse;
import com.example.scity.beens.NewsInfoBeen;
import com.example.scity.beens.PlListBeen;
import com.example.scity.beens.PostResponse;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.zip.Inflater;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class XinwenPlActivity extends AppCompatActivity {
    int id;
    PlListBeen plListBeen;
    ListView listView;
    Button button;
    EditText editText;
    PostResponse postResponse;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    DisplayLists();
                    break;
                case 2:
                    Toast.makeText(XinwenPlActivity.this,postResponse.msg,Toast.LENGTH_LONG).show();
                    LoadLists();
                    break;
            }
        }
    };

    private void DisplayLists() {
        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return plListBeen.rows.size();
            }

            @Override
            public Object getItem(int position) {
                return plListBeen.rows.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                convertView = LayoutInflater.from(XinwenPlActivity.this).inflate(R.layout.item_pllist,null);
                TextView textView = convertView.findViewById(R.id.pl_user);
                textView.setText(plListBeen.rows.get(position).userName);
                TextView textView1 = convertView.findViewById(R.id.pl_content);
                textView1.setText(plListBeen.rows.get(position).content);
                TextView textView2 = convertView.findViewById(R.id.pl_count);
                textView2.setText(plListBeen.rows.get(position).likeNum+"");
                TextView textView3 = convertView.findViewById(R.id.pl_sj);
                textView3.setText(plListBeen.rows.get(position).commentDate);
                return convertView;
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xinwen_pl);
        Intent intent = getIntent();
        id = intent.getIntExtra("ID",0);
        listView = findViewById(R.id.pl_lsts);
        LoadLists();
        button = findViewById(R.id.pl_btnpl);
        editText = findViewById(R.id.pl_edtpl);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Configs.Token.equals("")){
                    startActivity(new Intent(XinwenPlActivity.this,LoginActivity.class));
                }else{
                    OkHttpClient okHttpClient = new OkHttpClient();
                    HashMap<String,String> map = new HashMap<>();
                    map.put("newsId",id+"");
                    map.put("content",editText.getText().toString());
                    MediaType mediaType = MediaType.parse("application/json");//请求数据类型
                    RequestBody requestBody = RequestBody.create(mediaType,new Gson().toJson(map));//请求体
                    //请求对象
                    Request request = new Request.Builder().url(Configs.Host+APIInterface.FBPL)
                            .addHeader("Authorization",Configs.Token)
                            .post(requestBody).build();//
                    okHttpClient.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(@NonNull Call call, @NonNull IOException e) {

                        }

                        @Override
                        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                            String b = response.body().string();
                            Log.d(Configs.TAG,b);
                            postResponse = new Gson().fromJson(b, PostResponse.class);

                            handler.sendEmptyMessage(2);
                        }
                    });
                }
            }
        });
    }

    private void LoadLists() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Configs.Host+ APIInterface.PlLists +"?newsId=" +id).build();//http://124.93.196.45:10001/prod-api/press/press/29
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                plListBeen = new Gson().fromJson(response.body().string(), PlListBeen.class);
                handler.sendEmptyMessage(1);//加载成功
            }
        });
    }
}